#include "Image.h"
namespace NBGUI
{

	Image::Image(GUIDrawer* drawer,  const char* name, const char* eleText, int eleType, Rect& _rect,const char* path )
		:ElementNode(drawer, name,eleText,eleType,_rect),imagePath(path?path:"")
	{
		uv[0] = vec2f(0,0);
		uv[1] = vec2f(0,1);
		uv[2] = vec2f(1,1);
		uv[3] = vec2f(1,0);
		 
		 
		 
		 
	}


	Image::~Image()
	{

	}

	void Image::draw()
	{
		if(hide)
			return;
		pDrawer->pRenderer->applyTexture(imageId);
		pDrawer->pRenderer->drawIndexed(startIndex,indexLen,pDrawer->vboId,pDrawer->iboId);
		pDrawer->font->drawStrPack(textDrawingPack);
		//renderer->applyTexture(0);

		for(auto it = children.begin(); it != children.end(); ++it)
		{
			(*it)->draw();
		}
	}

	void Image::buildVI( std::vector<Vertex>* vertices, std::vector<unsigned int>* indices,unsigned int screenWidth, unsigned int screenHeight )
	{
		startIndex = indices->size();
		//just draw the textures
		float left, right, top, bottom;
		left = (-(float)screenWidth / 2) + (float)rect.x;
		right = left + (float)rect.w;
		top = (float)screenHeight / 2 - (float)rect.y;
		bottom = top - (float)rect.h;

		Vertex eleVertices[4];
		unsigned int currentVertexNum = vertices->size();
		unsigned int eleIndices[6]={0,1,2,0,2,3};

		eleVertices[0].pos = vec3f(left, top, 0.0f);  // Top left.
		eleVertices[0].coord = uv[0];

		eleVertices[1].pos = vec3f(left, bottom, 0.0f);  // Bottom left.
		eleVertices[1].coord = uv[1];

		eleVertices[2].pos = vec3f(right, bottom, 0.0f);  // Bottom right.
		eleVertices[2].coord = uv[2];

		eleVertices[3].pos = vec3f(right, top, 0.0f);  // Top right.
		eleVertices[3].coord = uv[3];

		for (int i = 0; i < 4; ++i)
		{
			vertices->push_back(eleVertices[i]);
		}



		for (int i = 0; i  < 6; ++i)
		{
			indices->push_back(currentVertexNum + eleIndices[i]);
		}

		indexLen += 6;
	}

	void Image::loadImg( RendererBase* pRenderer )
	{
		imageId = pRenderer->createTexture(imagePath.c_str());
	}

	void Image::doSomething( std::vector<info>& i, std::string& funcName )
	{
		if(funcName == std::string("setHide"))
			setHide( i.front().i == 1);
	}

}